package com.liwentao.seckill.exception;

import com.liwentao.seckill.result.R;
import com.liwentao.seckill.result.ResponseEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public R ExceptionHandler(Exception e){
        if(e instanceof GlobalException){
            GlobalException ex = (GlobalException) e;
            return R.error(ex.getResponseEnum());
        }else if(e instanceof BindException){
            BindException ex = (BindException) e;
            R respBean=R.error(ResponseEnum.BINDE_ERROR);
            respBean.setMessage("参数校验异常："+ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            return respBean;
        }
        return R.error(ResponseEnum.ERROR);
    }
}
